package gw.com.android.ui.trade.Fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigSettingDeal;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.presenter.AdsConfigPresenter;
import gw.com.android.ui.dialog.LoginDialog;
import gw.com.android.ui.trade.AdvertiseHandler;
import gw.com.android.utils.ColorThemeUtil;
import www.com.library.app.PushMsgTabFragment;
import www.com.library.model.DataItemDetail;
import www.com.library.util.CommonUtils;
import www.com.library.view.BtnClickListener;
import www.com.library.view.TintTextView;

/**
 * 交易失败界面
 * @author Administrator
 *
 */
@Deprecated
public class OrderFailFragment extends PushMsgTabFragment {

	/**顶部title,失败提示信息视图*/
	@BindView(R.id.res_status_title)
	TextView title;
	@BindView(R.id.res_status_pro)
	TextView proName;
	@BindView(R.id.res_status_content)
	TextView contentText;
	@BindView(R.id.btn_custom_left)
	TintTextView leftBtn;
	@BindView(R.id.btn_custom_right)
	TintTextView rightBtn;
	@BindView(R.id.iv_ad)
	ImageView ivAd;

	/**界面各参数对象集*/
	private DataItemDetail mDetail = new DataItemDetail();
	/**下单失败的编码*/
	private int failCode = 0;
	/**交易类型*/
	private String mCurTag = AppContances.TAB_ID_CREATE_ORDER;
	/**底部按钮点击监听对象*/
	private BtnClickListener mListener;
	private DataItemDetail mDepositDetail;
	private AdvertiseHandler adHandler;

	@Override
	public void setArguments(Bundle args) {
		super.setArguments(args);
		if (args != null) {
			mDetail = args.getParcelable("mDetail");
		}
	}

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_order_result_failure;
	}

	@Override
	protected void initLayoutView() {
		ButterKnife.bind(this, mRootView);
	}

	public void setData(DataItemDetail item){
		mDetail = item.Copy();
		initViewData();
	}

	@Override
	protected void initViewData() {
		ConfigSettingDeal mSettingDeal = new ConfigSettingDeal();
		mDepositDetail = mSettingDeal.getItemValue(ConfigType.DEPOSIT_TAG);
		mDetail.Dump();
		mCurTag = mDetail.getString("mCurrentTag");
		failCode = mDetail.getInt("failCode");

		leftBtn.setBackgroundResource(R.drawable.common_big_btn_bg);
		leftBtn.setColorValue(ColorThemeUtil.instance().color_e,ColorThemeUtil.instance().color_e);
		rightBtn.setVisibility(View.GONE);
		//市价开仓
		if (mCurTag.equals(AppContances.TAB_ID_CREATE_ORDER)) {
			title.setText(AppMain.getAppString(R.string.order_result_failure, AppMain.getAppString(R.string.order_type_create_market)));
			handleAd(AdsConfigPresenter.TYPE_CREATE_ORDER_FAILED);
			//挂单开仓
		} else if (mCurTag.equals(AppContances.TAB_ID_PENDING_ORDER)) {
			title.setText(AppMain.getAppString(R.string.order_result_failure, AppMain.getAppString(R.string.order_type_create_pending_fail)));
			//市价平仓
		} else if (mCurTag.equals(AppContances.TAB_ID_CLOSE_ORDER)) {
			title.setText(AppMain.getAppString(R.string.order_result_failure, AppMain.getAppString(R.string.order_type_close_market)));
			//挂单修改
		} else if (mCurTag.equals(AppContances.TAB_ID_PENDING_MODIFY_ORDER)) {
			title.setText(AppMain.getAppString(R.string.order_result_failure, AppMain.getAppString(R.string.order_type_modity_pending)));
			//止盈止损修改
		} else if (mCurTag.equals(AppContances.TAB_ID_MODIFY_ORDER)) {
			title.setText(AppMain.getAppString(R.string.order_result_failure, AppMain.getAppString(R.string.order_type_modify_market)));
		}

		String content = ConfigUtil.instance().getErrorConfigObject().optString(failCode+"");

		if (failCode == 89) {
			content = ConfigUtil.instance().getErrorConfigObject().optString(failCode+"").replace("%s","0.01");
		} else if(failCode == 39){
			rightBtn.setText(mDepositDetail.getString(ConfigType.CONFIG_TYPE_TITLE_TAG));
			if(GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_REAL){
				rightBtn.setVisibility(View.VISIBLE);
				leftBtn.setBackgroundResource(R.drawable.trade_back_btn_bg);
				leftBtn.setColorValue(ColorThemeUtil.instance().color_k,ColorThemeUtil.instance().color_e);
			}else{
				leftBtn.setBackgroundResource(R.drawable.common_big_btn_bg);
				leftBtn.setColorValue(ColorThemeUtil.instance().color_e,ColorThemeUtil.instance().color_e);
			}
		}
		if(!"".equals(content)){
			contentText.setText(content);
		}
		proName.setText(mDetail.getString("mSymbol"));
	}

	/**
	 * 设置按钮点击监听事件
	 */
	public void setBtnClickListener(BtnClickListener listener) {
		mListener = listener;
	}

	private void handleAd(String tag){
		if (adHandler == null){
			adHandler = new AdvertiseHandler(tag);
		}
		adHandler.handle(getActivity(),ivAd);
	}

	/**
	 * 返回上个界面的操作逻辑
	 */
	@OnClick(R.id.btn_custom_left)
	void goBack(View v) {
		if(CommonUtils.isFastDoubleClick()){
			return;
		}
		if (null != mListener) {
			mListener.onBtnClick(v.getId());
		}else{
			if (null != getActivity()) {
				getActivity().finish();
			}
		}
	}

	/**
	 * 上面按钮的操作逻辑
	 */
	@OnClick(R.id.btn_custom_right)
	void onViewOperate(View v) {
		if(CommonUtils.isFastDoubleClick()){
			return;
		}
		//游客登录时弹出提示
		if (GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_REAL) {
			LoginDialog.showLoginDialog(getActivity(), ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1014));
			return;
		}
		//存款按钮处理
		ActivityManager.showWebPageActivity(getActivity(), mDepositDetail, AppMain.getAppString(R.string.btn_back));
	}

	@OnClick(R.id.res_complete)
	void onComplete(View view){
		getActivity().finish();
	}

}
